package com.lfk.justwetools.Utils;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;
import ohos.agp.components.element.Element;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Component;
import ohos.agp.components.Text;

import com.lfk.justwetools.ResourceTable;


/**
 * 简化Toast的定制工具类
 * @author liufengkai
 */
public class ToastUtils {
    private Context context;
    private Builder mBuilder;
    private ToastDialog mToast;
    // 背景
    private Element mBackgroundDrawable;
    // 背景Id
    private int mBackgroundResId;
    /**
     * Show the view or text notification for a short period of time.  This time
     * could be user-definable.  This is the default.
     *
     * @see #setDuration
     */
    public static final int LENGTH_SHORT = 0;

    /**
     * Show the view or text notification for a long period of time.  This time
     * could be user-definable.
     *
     * @see #setDuration
     */
    public static final int LENGTH_LONG = 1;

    /**
     * 初始化Toast加载默认样式
     * @param context
     */
    public ToastUtils(Context context) {
        this.context = context;
        mBuilder = new Builder();
//        Log.e("===>","toastutils");
    }

    /**
     * Builder
     */
    private class Builder {
        private LayoutScatter inflater = LayoutScatter.getInstance(context);
        private Component view;
        private Text textView;

        private Builder() {
//            Log.e("===>","builder");
            view = inflater.parse(ResourceTable.Layout_toast_item, null, false);
            textView = (Text) view.findComponentById(ResourceTable.Id_toast_text);
            mToast = new ToastDialog(context);
            mToast.setAlignment(LayoutAlignment.HORIZONTAL_CENTER | LayoutAlignment.CENTER);
            mToast.setDuration(200);
            mToast.setComponent(view);
        }

        public void setText(String text) {
            textView.setText(text);
//            Log.e("===>", "text");
        }

        public void setBackground(Element drawable) {
            view.setBackground(drawable);
//            mToast.setView(view);
        }

        public void setBackground(int resId) {
            view.setBackground(ElementScatter.getInstance(context).parse(resId));
//            mToast.setView(view);
        }

        public void setGravity(int gravity, int xOffset, int yOffset) {
            mToast.setAlignment(gravity);
        }

        public void setDuration(int duration) {
            mToast.setDuration(duration);
        }

        public void setView(Component view) {
            this.view = view;
            mToast.setComponent(view);
        }

        public void setView(Component view, int res) {
            this.view = view;
            this.textView = (Text) view.findComponentById(res);
            mToast.setComponent(view);
        }

        public void setColor(int color) {
            ShapeElement element = new ShapeElement();
            element.setRgbColor(RgbColor.fromArgbInt(color));
            view.setBackground(element);
//            Log.e("===>", "color");
        }

        public void setTextColor(int color) {
            textView.setTextColor(new Color(color));
        }
    }

    public ToastUtils show() {
//        Log.e("===>","show");
        if (mBuilder == null)
            mBuilder = new Builder();
        mToast.show();
        return this;
    }

    public ToastUtils setBackground(Element drawable) {
        mBackgroundDrawable = drawable;
        if (mBuilder != null) {
            mBuilder.setBackground(mBackgroundDrawable);
        } else {
            mBuilder = new Builder();
            mBuilder.setBackground(mBackgroundDrawable);
        }
        return this;
    }

    public ToastUtils setBackground(int resId) {
        mBackgroundResId = resId;
        if (mBuilder != null) {
            mBuilder.setBackground(resId);
        } else {
            mBuilder = new Builder();
            mBuilder.setBackground(resId);
        }
        return this;
    }

    public ToastUtils setText(String text) {
        if (mBuilder != null) {
            mBuilder.setText(text);
        } else {
            mBuilder = new Builder();
            mBuilder.setText(text);
        }
        return this;
    }

    public ToastUtils setGravity(int gravity, int xOffset, int yOffset) {
        if (mBuilder != null) {
            mBuilder.setGravity(gravity, xOffset, yOffset);
        } else {
            mBuilder = new Builder();
            mBuilder.setGravity(gravity, xOffset, yOffset);
        }
        return this;
    }

    public ToastUtils setDuration(int duration) {
        if (mBuilder != null) {
            mBuilder.setDuration(duration);
        } else {
            mBuilder = new Builder();
            mBuilder.setDuration(duration);
        }
        return this;
    }

    public ToastUtils setView(Component view) {
        if (mBuilder != null) {
            mBuilder.setView(view);
        } else {
            mBuilder = new Builder();
            mBuilder.setView(view);
        }
        return this;
    }

    /**
     * 设定View和与之对应的资源文件
     * @param view
     * @param res
     * @return
     */
    public ToastUtils setView(Component view, int res) {
        if (mBuilder != null) {
            mBuilder.setView(view, res);
        } else {
            mBuilder = new Builder();
            mBuilder.setView(view, res);
        }
        return this;
    }

    public ToastUtils setColor(int color) {
        if (mBuilder != null) {
            mBuilder.setColor(color);
        } else {
            mBuilder = new Builder();
            mBuilder.setColor(color);
        }
        return this;
    }

    public ToastUtils setTextColor(int color) {
        if (mBuilder != null) {
            mBuilder.setTextColor(color);
        } else {
            mBuilder = new Builder();
            mBuilder.setTextColor(color);
        }
        return this;
    }
}
